﻿#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace CherryTomato.Entities
{

    #region Delegates

    public delegate void SelectedIndexChangedEventHandler(object sender, EventArgs e);

    #endregion Delegates

    public class MovieSearchResults : ICollection<Movie>
    {
        #region Fields

        /// <summary>
        ///     Gets or sets the index of the currently selected index
        /// </summary>
        private int selectedIndex;

        #endregion Fields

        #region Constructors

        /// <summary>
        ///     Default Constructor
        /// </summary>
        public MovieSearchResults()
        {
            ResultCount = 0;
            Movies = new List<Movie>();
            selectedIndex = 0;
        }

        #endregion Constructors

        #region Events

        public event SelectedIndexChangedEventHandler SelectedIndexChanged;

        #endregion Events

        #region Properties

        /// <summary>
        ///     Total Number Of Results
        /// </summary>
        public int ResultCount { get; set; }

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                OnSelectedIndexChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        ///     Gets the currently selected item
        /// </summary>
        public Movie SelectedValue
        {
            get { return Movies.ElementAt(SelectedIndex); }
        }

        /// <summary>
        ///     List Of Movie Results
        /// </summary>
        private List<Movie> Movies { get; set; }

        public int Count
        {
            get { return Movies.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        #endregion Properties

        #region Indexers

        /// <summary>
        ///     List indexer
        /// </summary>
        /// <param name="index"> </param>
        /// <returns> </returns>
        public Movie this[int index]
        {
            get { return Movies[index]; }
            set { Movies[index] = value; }
        }

        #endregion Indexers

        #region Methods

        public void Add(Movie item)
        {
            Movies.Add(item);
        }

        public void Clear()
        {
            Movies.Clear();
        }

        public bool Contains(Movie item)
        {
            return Movies.Contains(item);
        }

        public void CopyTo(Movie[] array, int arrayIndex)
        {
            Movies.CopyTo(array, arrayIndex);
        }

        public IEnumerator<Movie> GetEnumerator()
        {
            return Movies.GetEnumerator();
        }

        public bool Remove(Movie item)
        {
            return Movies.Remove(item);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public virtual void OnSelectedIndexChanged(EventArgs e)
        {
            if (SelectedIndexChanged != null)
                SelectedIndexChanged(this, e);
        }

        /// <summary>
        ///     Sorts the Movies by their ID
        /// </summary>
        public void Sort()
        {
            Movies.Sort();
        }

        /// <summary>
        ///     Sorts the Movies by their ID
        /// </summary>
        /// <param name="comparer"> Comparer to implement for sorting </param>
        public void Sort(IComparer<Movie> comparer)
        {
            Movies.Sort(comparer);
        }

        #endregion Methods
    }
}